package csc.fresher.view;

import java.io.Serializable;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import java.util.Calendar;
import csc.fresher.entity.DepositTransaction;
import csc.fresher.service.InternetTransactionService;

@ManagedBean
@SessionScoped
public class InternetTransactionManageBean implements Serializable{
	private ArrayList<Long> listAccountNumber;
	//private Pagination pagination;
	private long accountNumber;
	private int numberOfTransaction = 0;
	
	@ManagedProperty(value = "#{internetTransactionServiceImpl}")
	private InternetTransactionService internetTransactionService;

	@ManagedProperty(value = "#{internetLoginManageBean}")
	private InternetLoginManageBean loginBean;
	

	private boolean showOnlyInternetTransaction = false;
	
	private String fromDate;

	private String toDate;
	private ArrayList<DepositTransaction> transactionList;

	
	@PostConstruct
	public void init() {
		setListAccountNumber(internetTransactionService
				.getAccountNumber(loginBean.getAcc().getCustomerID()));
		Date timeNow = new Date(Calendar.getInstance().getTimeInMillis());
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		toDate = format.format(timeNow);
		fromDate = format.format(new Date(timeNow.getTime()-30*24*60*60*1000L));
	}

	public ArrayList<Long> getListAccountNumber() {
		return listAccountNumber;
	}

	public void setListAccountNumber(ArrayList<Long> listAccountNumber) {
		this.listAccountNumber = listAccountNumber;
	}

	public InternetTransactionService getInternetTransactionService() {
		return internetTransactionService;
	}

	public void setInternetTransactionService(
			InternetTransactionService internetTransactionService) {
		this.internetTransactionService = internetTransactionService;
	}

	public InternetLoginManageBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(InternetLoginManageBean loginBean) {
		this.loginBean = loginBean;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String searchTransaction() {
		try {
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			Date frommDateFormat = new java.sql.Date(format.parse(fromDate)
					.getTime());
			Date toDateFormat = new java.sql.Date(format.parse(toDate)
					.getTime());

			toDateFormat = new java.sql.Date(format.parse(toDate).getTime());
			numberOfTransaction = internetTransactionService.getNumberOfTransaction(accountNumber, frommDateFormat, toDateFormat, showOnlyInternetTransaction);
			//pagination.setCurrentPage(1);
			//pagination.setTotalPage((int) Math.ceil(numberOfTransaction * 1.0 /pagination.getPerPage()));
			transactionList = (ArrayList<DepositTransaction>) internetTransactionService
					.getTransaction(accountNumber, frommDateFormat,
							toDateFormat, showOnlyInternetTransaction, 0, numberOfTransaction);
			
			
			return "transactiondetail";
		} catch (ParseException e) {
			return "transactiondetail";
		}
	}
	
	/*
	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
*/
	/*
	public String gotoPage() {
		try {
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			Date frommDateFormat = new java.sql.Date(format.parse(fromDate)
			Date toDateFormat = new java.sql.Date(format.parse(toDate)
					.getTime());
			;
			toDateFormat = new java.sql.Date(format.parse(toDate).getTime());
			transactionList = (ArrayList<DepositTransaction>) internetTransactionService
					.getTransaction(accountNumber, frommDateFormat,
							toDateFormat, showOnlyInternetTransaction, (pagination.getCurrentPage()-1)*pagination.getPerPage(), pagination.getPerPage());
			
			
			return "transactiondetail";
		} catch (ParseException e) {
			return "transactiondetail";
		}
	}
	*/
	

	public long getAccountNumber() {
		return accountNumber;
	}

	public void setAccountNumber(long accountNumber) {
		this.accountNumber = accountNumber;
	}

	public ArrayList<DepositTransaction> getTransactionList() {
		return transactionList;
	}

	public void setTransactionList(ArrayList<DepositTransaction> transactionList) {
		this.transactionList = transactionList;
	}

	public int getNumberOfTransaction() {
		return numberOfTransaction;
	}

	public void setNumberOfTransaction(int numberOfTransaction) {
		this.numberOfTransaction = numberOfTransaction;
	}

	public boolean isShowOnlyInternetTransaction() {
		return showOnlyInternetTransaction;
	}

	public void setShowOnlyInternetTransaction(boolean showOnlyInternetTransaction) {
		this.showOnlyInternetTransaction = showOnlyInternetTransaction;
	}

}
